package middlewares

import (
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/models/passenger"
	"github.com/gofiber/fiber/v2"
	"github.com/golang-jwt/jwt/v5"
	"github.com/spf13/cast"
)

func Passenger() func(c *fiber.Ctx) error {
	return func(c *fiber.Ctx) error {
		user := c.Locals("user").(*jwt.Token)
		claims := user.Claims.(jwt.MapClaims)
		userId := cast.ToUint64(claims["user_id"].(string))
		// 查询乘客信息

		if p := passenger.GetByUserId(userId); p.ID > 0 {
			c.Locals("passenger", p)
			return c.Next()
		}

		return c.Status(fiber.StatusUnprocessableEntity).JSON(map[string]interface{}{
			"message": "请先成为乘客",
			"data":    map[string]interface{}{},
		})
	}
}
